﻿using System.Collections.Generic;
using UnityEngine;
using VoxelBlock.VoxelBlock3D;
using VoxelBlock;using Vector3Int = VoxelBlock.Vector3Int;

public class OperationImplementRectanglePaint : OperationImplement
{

    IList<SimpleVoxel> oldSerializeVoxel = new List<SimpleVoxel>();
    Vector3Int rectangleOldPosition;


    //public OperationImplementRectanglePaint(ProxyOperation proxy) : base(proxy)
    //{

    //}

    public override void Start(StartArg arg)
    {
        base.Start(arg);

    }

    public override void Run(RunArg arg)
    {
        if (rectangleOldPosition != arg.nowSelectPosition)
        {
            //Vector3Int positionA = gestureStartPosition;
            //rectangleOldPosition = nowSelectPosition;

            //var arrayPoint = BasicAlgorithm.GetRectanglePointArray(positionA, rectangleOldPosition);
            //Voxel3D[] addArray = new Voxel3D[oldSerializeVoxel.Count];
            //for (int i = 0, max = oldSerializeVoxel.Count; i < max; i++)
            //{
            //    addArray[i] = new Voxel3D(oldSerializeVoxel[i].position, oldSerializeVoxel[i].uvKey);
            //}
            //nowDrawVoxel3DCore.DrawAddBlock(addArray);
            //oldSerializeVoxel = nowDrawVoxel3DCore.DrawPaintBlock(arrayPoint, nowSelectUVKey);

            rectangleOldPosition = arg.nowSelectPosition;

            var arrayPoint = BasicAlgorithm.GetRectanglePointArray(arg.gestureStartPosition, rectangleOldPosition);

            nowDrawVoxel3DCore.DrawPaintBlock(historyRecordFrontList);
            var data = nowDrawVoxel3DCore.DrawPaintBlock(arrayPoint, arg.nowSelectUVKey);
            
            historyRecordFrontList = data.Item1;
            historyRecordBreakList = data.Item2;
        }
    }

    public override void Over(OverArg arg)
    {
        if (isStop)
        {
            if (historyRecordFrontList.Count > 0 || historyRecordBreakList.Count > 0)
            {
                proxy.Write(VoxelCategories.Operation.Paint, historyRecordFrontList, historyRecordBreakList);
                historyRecordFrontList = new List<SimpleVoxel>();
                historyRecordBreakList = new List<SimpleVoxel>();
            }
        }
        else
        {
            if (historyRecordFrontList.Count > 0 || historyRecordBreakList.Count > 0)
            {
                proxy.Write(VoxelCategories.Operation.Paint, historyRecordFrontList, historyRecordBreakList);
                historyRecordFrontList = new List<SimpleVoxel>();
                historyRecordBreakList = new List<SimpleVoxel>();
            }
            else
            {
                var data = nowDrawVoxel3DCore.DrawPaintBlock(new Vector3Int[] { arg.nowSelectPosition }, arg.nowSelectUVKey);
                proxy.Write(VoxelCategories.Operation.Paint, data.Item1, data.Item2);
            }
        }
    }

}


